sysroot: Add an API to lock
authorColin Walters <walters@verbum.org>
Mon, 4 May 2015 22:35:52 +0000 (18:35 -0400)
committerColin Walters <walters@verbum.org>
Tue, 5 May 2015 12:52:44 +0000 (08:52 -0400)
If a system administrator happens to type `ostree admin upgrade`
multiple times, currently that will lead to a potentially corrupted
system.

I originally attempted to do locking *internally* in `libostree`, but
that didn't work out because currently a number of the commands
perform multi-step operations that all need to be serialized.  All of
the current code in `ostree admin deploy` is an example.

Therefore, allow callers to perform locking, as most of the higher
level logic is presently implemented there.

At some point, we can revisit having internal locking, but it will be
difficult.  A more likely approach would be similar to Java's approach
with concurrency on iterators - a "fail fast" method.

Makefile-tests.am
doc/ostree-sections.txt
libglnx
src/libostree/ostree-sysroot-private.h
src/libostree/ostree-sysroot.c
src/libostree/ostree-sysroot.h
src/ostree/ot-admin-builtin-deploy.c
tests/test-admin-locking.sh [new file with mode: 0644]

index c697333965ed14f40a2ada8bd05868688bf3af8e..bf584240dfc85aa1f5cee1d4bdfece01ff7d95d8 100644 (file)
@@ -45,6 +45,7 @@ testfiles = test-basic \
        test-admin-deploy-uboot \
        test-admin-instutil-set-kargs \
        test-admin-upgrade-not-backwards \
+       test-admin-locking \
        test-repo-checkout-subpath      \
        test-reset-nonlinear \
        test-setuid \
index d26d37a4f26d6889fa974006fd3c6d44e1e272db..9e2ef6c35eee741607c04bc4b22781b1623dba66 100644 (file)
@@ -365,6 +365,8 @@ ostree_sysroot_new
 ostree_sysroot_new_default
 ostree_sysroot_get_path
 ostree_sysroot_load
+ostree_sysroot_lock
+ostree_sysroot_unlock
 ostree_sysroot_get_fd
 ostree_sysroot_ensure_initialized
 ostree_sysroot_get_bootversion
diff --git a/libglnx b/libglnx
index dfe77be2d558373c4b01189e2048d79be9c9c453..2558e0ea35712aed27234fd1ad4a5bac4fae3817 160000 (submodule)
--- a/libglnx
+++ b/libglnx
@@ -1 +1 @@
-Subproject commit dfe77be2d558373c4b01189e2048d79be9c9c453
+Subproject commit 2558e0ea35712aed27234fd1ad4a5bac4fae3817
index 69310c3f6eec7d66c57a6ce697729237846ccaab..fa7b0866ab1f9d14d290550247f8ef97c06e03ed 100644 (file)
@@ -20,6 +20,7 @@
 
 #pragma once
 
+#include "libglnx.h"
 #include "ostree.h"
 #include "ostree-kernel-args.h"
 #include "ostree-bootloader.h"
@@ -31,6 +32,7 @@ struct OstreeSysroot {
 
   GFile *path;
   int sysroot_fd;
+  GLnxLockFile lock;
 
   gboolean loaded;
   
@@ -43,8 +45,11 @@ struct OstreeSysroot {
 
   /* Only access through ostree_sysroot_get_repo() */
   OstreeRepo *repo;
+
 };
 
+#define OSTREE_SYSROOT_LOCKFILE "ostree/lock"
+
 gboolean
 _ostree_sysroot_read_boot_loader_configs (OstreeSysroot *self,
                                           int            bootversion,
index 62558037c2b5ade775a6dbf2e9cab0c3bec4af66..8f2b249634f8ec829af3043cded270b7f2b64aca 100644 (file)
@@ -44,6 +44,10 @@ find_booted_deployment (OstreeSysroot       *self,
  * which in particular should contain a toplevel /ostree directory.
  * Inside this directory is an #OstreeRepo in /ostree/repo, plus a set
  * of deployments in /ostree/deploy.
+ *
+ * This class is not by default safe against concurrent use by threads
+ * or external processes.  You can use ostree_sysroot_lock() to
+ * perform locking externally.
  */
 typedef struct {
   GObjectClass parent_class;
@@ -66,6 +70,8 @@ ostree_sysroot_finalize (GObject *object)
   g_clear_object (&self->sepolicy);
   g_clear_object (&self->repo);
 
+  glnx_release_lock_file (&self->lock);
+
   if (self->sysroot_fd != -1)
     (void) close (self->sysroot_fd);
 
@@ -148,6 +154,7 @@ static void
 ostree_sysroot_init (OstreeSysroot *self)
 {
   self->sysroot_fd = -1;
+  self->lock = (GLnxLockFile)GLNX_LOCK_FILE_INIT;
 }
 
 /**
@@ -1142,6 +1149,43 @@ ostree_sysroot_origin_new_from_refspec (OstreeSysroot  *sysroot,
   return ret;
 }
 
+/**
+ * ostree_sysroot_lock:
+ * @self: Self
+ * @error: Error
+ *
+ * Acquire an exclusive multi-process write lock for @self.  This call
+ * blocks until the lock has been acquired.  The lock is not
+ * reentrant.
+ *
+ * Release the lock with ostree_sysroot_unlock().  The lock will also
+ * be released if @self is deallocated.
+ */
+gboolean
+ostree_sysroot_lock (OstreeSysroot     *self,
+                     GError           **error)
+{
+  if (!ensure_sysroot_fd (self, error))
+    return FALSE;
+  return glnx_make_lock_file (self->sysroot_fd, OSTREE_SYSROOT_LOCKFILE,
+                              LOCK_EX, &self->lock, error);
+}
+
+/**
+ * ostree_sysroot_unlock:
+ * @self: Self
+ * @error: Error
+ *
+ * Clear the lock previously acquired with ostree_sysroot_lock().  It
+ * is safe to call this function if the lock has not been previously
+ * acquired.
+ */
+void
+ostree_sysroot_unlock (OstreeSysroot  *self)
+{
+  glnx_release_lock_file (&self->lock);
+}
+
 /**
  * ostree_sysroot_simple_write_deployment:
  * @sysroot: Sysroot
index fe6441fb14086e1b1c4ca7ff48c35f912b54d5a6..e7f6e482984b68e992991852708b13ba4445e1d3 100644 (file)
@@ -62,6 +62,9 @@ char *ostree_sysroot_get_deployment_dirpath (OstreeSysroot    *self,
 
 GFile * ostree_sysroot_get_deployment_origin_path (GFile   *deployment_path);
 
+gboolean ostree_sysroot_lock (OstreeSysroot  *self, GError **error);
+void ostree_sysroot_unlock (OstreeSysroot  *self);
+
 gboolean ostree_sysroot_cleanup (OstreeSysroot       *self,
                                  GCancellable        *cancellable,
                                  GError             **error);
index 091a7eac3e981c16e52164881badd5aa5e20db28..1872894b4b8042bb1f083142a46c115586feed1a 100644 (file)
@@ -79,6 +79,9 @@ ot_admin_builtin_deploy (int argc, char **argv, GCancellable *cancellable, GErro
 
   refspec = argv[1];
 
+  if (!ostree_sysroot_lock (sysroot, error))
+    goto out;
+
   if (!ostree_sysroot_load (sysroot, cancellable, error))
     goto out;
 
@@ -172,6 +175,8 @@ ot_admin_builtin_deploy (int argc, char **argv, GCancellable *cancellable, GErro
 
   ret = TRUE;
  out:
+  if (sysroot)
+    ostree_sysroot_unlock (sysroot);
   if (origin)
     g_key_file_unref (origin);
   if (context)
diff --git a/tests/test-admin-locking.sh b/tests/test-admin-locking.sh
new file mode 100644 (file)
index 0000000..b6cd7bd
--- /dev/null
@@ -0,0 +1,49 @@
+#!/bin/bash
+#
+# Copyright (C) 2015 Colin Walters <walters@verbum.org>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+# Boston, MA 02111-1307, USA.
+
+set -e
+
+. $(dirname $0)/libtest.sh
+
+echo "1..1"
+
+setup_os_repository "archive-z2" "syslinux"
+
+echo "ok setup"
+
+echo "1..1"
+
+cd ${test_tmpdir}
+${CMD_PREFIX} ostree --repo=sysroot/ostree/repo remote add --set=gpg-verify=false testos $(cat httpd-address)/ostree/testos-repo
+${CMD_PREFIX} ostree --repo=sysroot/ostree/repo pull testos testos/buildmaster/x86_64-runtime
+rev=$(${CMD_PREFIX} ostree --repo=sysroot/ostree/repo rev-parse testos/buildmaster/x86_64-runtime)
+export rev
+echo "rev=${rev}"
+# This initial deployment gets kicked off with some kernel arguments 
+${CMD_PREFIX} ostree admin --sysroot=sysroot deploy --karg=root=LABEL=MOO --karg=quiet --os=testos testos:testos/buildmaster/x86_64-runtime
+assert_has_dir sysroot/boot/ostree/testos-${bootcsum}
+
+count=$(($(getconf _NPROCESSORS_ONLN) * 2))
+seq "${count}" | parallel --no-notice -n0 ${CMD_PREFIX} ostree admin --sysroot=sysroot deploy --retain --os=testos testos:testos/buildmaster/x86_64-runtime
+
+${CMD_PREFIX} ostree admin --sysroot=sysroot status > status.txt
+grep "testos ${rev}" status.txt | wc -l > status-matches.txt
+assert_file_has_content status-matches.txt $((${count} + 1))
+
+echo 'ok deploy locking'